﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Portal.Web.Areas.Admin.Controllers
{
    public class LinkController : BaseAdminController
    {
        private readonly ILinkRepository _linkRepository;

        public LinkController(ILinkRepository linkRepository)
        {
            _linkRepository = linkRepository;
        }

        // GET: /Admin/Link/

        public ActionResult Index()
        {
            _linkRepository.Get();
            return View();
        }

        public ActionResult GetLinkJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_linkRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }
        public ActionResult Create()
        {
            var model = _linkRepository.CreateEntity();
            return View(model);
        }
        [HttpPost]
        [ValidateInput(false)]
        [CacheResetFilterAttribute(CacheKey.LinkList)]
        public ActionResult Create(FormCollection formCollection, Link entity, HttpPostedFileBase linkImage)
        {
            var model = _linkRepository.CreateEntity();
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.Name = entity.Name;
            model.HrefUrl = entity.HrefUrl;
            if (linkImage != null)
            {
                var upload = new UploadHelper(linkImage, "/Upload/Link/");
                upload.Save();
                model.Image = upload.FileRelativePath;
            }
            _linkRepository.Add(model);
            _linkRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "添加成功", state = false };
            return View(model);
        }

        public ActionResult Edit(string id)
        {
            var model = _linkRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            return View(model);
        }
        [HttpPost]
        [CacheResetFilterAttribute(CacheKey.LinkList)]
        public ActionResult Edit(Link entity, HttpPostedFileBase linkImage)
        {
            var model = _linkRepository.Get().SingleOrDefault(o => o.Id == entity.Id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            model.Name = entity.Name;
            model.HrefUrl = entity.HrefUrl;
            if (linkImage != null)
            {
                var upload = new UploadHelper();
                upload.ReplaceFile(linkImage, "/Upload/Link/", model.Image);
                model.Image = upload.FileRelativePath;
            }
            _linkRepository.Update(model);
            _linkRepository.Save();
            ViewData["Result"] = new AjaxResult { message = "修改成功", state = false };
            return View(model);
        }
        [HttpPost]
        [CacheResetFilterAttribute(CacheKey.LinkList)]
        public ActionResult Delete(List<string> id, FormCollection collection)
        {
            var list = _linkRepository.Get(o => id.Contains(o.Id));
            list.ForEach(o => _linkRepository.Delete(o));
            _linkRepository.Save();
            return Json(new AjaxResult { state = true, message = "删除成功" });
        }

    }
}
